﻿using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;


namespace DAL
{
    public class D_Group
    {
        public List<StuGroup> getList()
        {
            var list = SqlSugarHelper.Db.Queryable<StuGroup>().OrderBy(s_g => s_g.teacher_id).ToList();
            return list;
        }

        public bool saveList(List<StuGroup> list) 
        {
            var count = SqlSugarHelper.Db.Storageable<StuGroup>(list).ExecuteCommand();
            return count == list.Count ? true : false;
        }

        public StuGroup getGroup(Student student)
        {
            if (student.group_id == 0)
                return null;

            var group = SqlSugarHelper.Db.Queryable<StuGroup>()
                            .Where(s=>s.group_id == student.group_id).ToList().First();
            return group;
        }

        public int add(StuGroup group)
        {
            int it = SqlSugarHelper.Db.Insertable<StuGroup>(group).ExecuteReturnIdentity();
            // g_data重命名成StuChoice
            StuChoice choice = new StuChoice();
            choice.Group_id = it;
            SqlSugarHelper.Db.Insertable(choice).ExecuteCommand();
            return it;
        }

        public bool update(StuGroup group)
        {
            var count = SqlSugarHelper.Db.Updateable<StuGroup>(group).ExecuteCommand();
            return count != 0 ? true : false;
        }

        public bool release(StuGroup group)
        {
            var count = SqlSugarHelper.Db.Deleteable<StuGroup>(group).ExecuteCommand();
            return count != 0 ? true : false;
        }
    }
}
